package com.huaxin.hxcloud.find;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.Entity.BaseDTO;
import com.huaxin.common.Entity.FindInfo;
import com.huaxin.common.Global;
import com.huaxin.common.utils.DialogUtil;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.find.view.HxCommentContentEventBus;

import org.greenrobot.eventbus.EventBus;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hebing on 2017/4/21.
 */

public class SendCommentActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.activity_comment_find_edt)
    EditText editText;
    @BindView(R.id.toolbar)
    Toolbar  toolbar;
    @BindView(R.id.find_commit_giveup)
    TextView giveupCommit;
    @BindView(R.id.find_commit)
    TextView commitText;
    private int questionId;
    private int toUserId;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_comment_find;
    }

    @Override
    public void initView() {
        super.initView();
        giveupCommit.setOnClickListener(this);
        commitText.setOnClickListener(this);
        //
        questionId = getIntent().getIntExtra("questionId", 0);
        toUserId = getIntent().getIntExtra("toUserId", 0);
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    protected void requestData() {
        super.requestData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        showKeyboard();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.find_commit_giveup:
                finish();
                break;
            case R.id.find_commit:
                hideIme();
                sendComment();
                break;
        }
    }

    private void showKeyboard() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(editText, 0);
            }
        }, 300);
    }


    private void hideIme() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context
                .INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive(editText)) {
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    private void sendComment() {
        final Dialog dialog = DialogUtil.getWaitDialog(this, "提交中");
        dialog.show();
        if (TextUtils.isEmpty(editText.getText().toString().trim())) {
            ToastUtil.showToastShort("评论不能空");
        }
        Call<BaseDTO> call = getRequestApi().commitComment(questionId, editText.getText().toString(), 1, toUserId, null);
        autoClose(call);
        call.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
                dialog.dismiss();
                if (response.isSuccessful()) {
                    ToastUtil.showToastShort("发表成功");
                    Intent intent = new Intent();
                    intent.putExtra("code", 1);
                    setResult(RESULT_OK, intent);

                    FindInfo.QuestionListBean.PartialTopCommentArrBean commentArrBean = new FindInfo.QuestionListBean.PartialTopCommentArrBean();
                    commentArrBean.setContent(editText.getText().toString().trim());
                    FindInfo.QuestionListBean.PartialTopCommentArrBean.PublisherBeanX publisherBeanX = new FindInfo.QuestionListBean.PartialTopCommentArrBean.PublisherBeanX();
                    publisherBeanX.setId(Global.getUser().getUserId());
                    publisherBeanX.setImg(Global.getUser().getImgUrl());
                    publisherBeanX.setNickName(Global.getUser().getNickName());
                    commentArrBean.setPublisher(publisherBeanX);
                    EventBus.getDefault().post(new HxCommentContentEventBus(commentArrBean, questionId, HxCommentContentEventBus.ADD_COMMENT));
                    finish();
                } else {
                    ToastUtil.showToastShort("提交失败");
                }
            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                dialog.dismiss();
                ToastUtil.showToastShort("网络故障");
            }
        });

    }
}
